package com.android.baseconfig.base;

import android.app.Dialog;
import android.content.Intent;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import com.android.baseconfig.R;
import com.android.baseconfig.common.helper.NetObserverManager;
import com.android.baseconfig.common.observer.NetConnectionObserver;
import com.android.baseconfig.common.utils.DialogUtils;
import com.android.baseconfig.common.utils.Logc;
import com.android.baseconfig.common.utils.StatusBarUtil;

import androidx.appcompat.app.AppCompatActivity;
import butterknife.ButterKnife;

/**
 * Activity基类
 */
public abstract class BaseActivity<V extends BaseView, T extends BasePresenter<V>>
        extends AppCompatActivity implements NetConnectionObserver {
    protected T presenter;
    protected View view;
    Dialog dialog;
    protected static final String TAG = BaseActivity.class.getSimpleName();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //判断app状态
        // if (AppStatusManager.getInstance().getAppStatus() == AppStatus.STATUS_RECYCLE){
        //     //被回收，跳转到启动页面
        //     Intent intent = new Intent(this, StartActivity.class);
        //     startActivity(intent);
        //     finish();
        //     return;
        // }

        setStatusBar();
        NetObserverManager.getInstance().addNetObserver(this);
        int resourceId = 0;
        if ((resourceId = resourceId()) != 0) {
            setContentView(resourceId);
            ButterKnife.bind(this);
        }
        initPresenter();
        initView();
        initData();
    }

    private void setStatusBar() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            if (isUseFullScreenMode()) {
                StatusBarUtil.transparencyBar(this);
            } else {
                StatusBarUtil.setStatusBarColor(this, getStatusBarColor());
            }

            if (isUseBlackFontWithStatusBar()) {
                StatusBarUtil.setLightStatusBar(this, true, isUseFullScreenMode());
            }
        }
    }

    protected void setStatusBarColor(boolean isUseBlack) {
        if (isUseFullScreenMode()) {
            StatusBarUtil.transparencyBar(this);
        } else {
            StatusBarUtil.setStatusBarColor(this, getStatusBarColor());
        }

        if (isUseBlack) {
            StatusBarUtil.setLightStatusBar(this, true, isUseFullScreenMode());
        }
    }

    /**
     * 是否设置成透明状态栏，即就是全屏模式
     */
    protected boolean isUseFullScreenMode() {
        return true;
    }

    /**
     * 更改状态栏颜色，只有非全屏模式下有效
     */
    protected int getStatusBarColor() {
        return R.color.white;
    }

    /**
     * 是否改变状态栏文字颜色为黑色，默认为黑色
     */
    protected boolean isUseBlackFontWithStatusBar() {
        return true;
    }

    protected abstract int resourceId();

    public T getPresenter() {
        return presenter;
    }

    private void initPresenter() {
        presenter = createPresenter();
        if (presenter != null) {
            presenter.attachView((V) this);
            presenter.init();
            presenter.start();
        }
    }
    protected abstract T createPresenter();

    protected void initView() {}

    // protected abstract View initView(LayoutInflater inflater);

    protected void initData() {}

    public void tips(final String msg) {
        this.runOnUiThread(() -> {
            Toast tt = Toast.makeText(BaseActivity.this, msg, Toast.LENGTH_SHORT);
            tt.setText(msg);
            tt.show();
        });
    }

    @Override
    public boolean moveTaskToBack(boolean nonRoot) {
        return super.moveTaskToBack(true);
    }

    @Override
    protected void onStart() {
        super.onStart();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        NetObserverManager.getInstance().removeNetObserver(this);
        super.onDestroy();
        if (presenter != null) {
            presenter.destroy();
            presenter.detachView();
        }
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        if (newConfig.fontScale != 1) {
            getResources();
        }
    }

    /**
     * 禁止改变字体大小
     */
    @Override
    public Resources getResources() {
        Resources res = super.getResources();
        Configuration config = new Configuration();
        config.setToDefaults();
        res.updateConfiguration(config, res.getDisplayMetrics());
        return res;
    }

    @Override
    public void updateNetStatus(int type) {

    }

    @Override
    public void updateNFCStatus(int state) {

    }

    @Override
    public void updateWifiStatus(int state) {

    }

    @Override
    public void startActivity(Intent intent) {
        super.startActivity(intent);
        overridePendingTransition(R.anim.activity_open_enter, R.anim.activity_open_exit);
    }

    @Override
    public void startActivityForResult(Intent intent, int requestCode) {
        super.startActivityForResult(intent, requestCode);
        overridePendingTransition(R.anim.activity_open_enter, R.anim.activity_open_exit);
    }

    @Override
    public void finish() {
        super.finish();
        overridePendingTransition(R.anim.activity_close_enter, R.anim.activity_close_exit);
    }

    public void showLoading(String tips, boolean isCanCancel) {
        if (null != dialog && dialog.isShowing()) {
            try {
                dialog.dismiss();
            } catch (Exception e) {
                e.printStackTrace();
            }
            dialog = null;
        }
        dialog = DialogUtils.createLoadingDialog(this, tips);
        dialog.setCancelable(isCanCancel);
        dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
        if (!isFinishing()) {
            dialog.show();
        }
    }

    public void showLoading() {
        showLoading("", true);
    }

    public void closeLoading() {
        if (null != dialog && dialog.isShowing()) {
            try {
                dialog.dismiss();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public void showToast(String data) {
        // ToastUtils.getInstance()
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        try {
            super.onRestoreInstanceState(savedInstanceState);
        } catch (Exception e) {
            e.printStackTrace();
        }
        //TODO 用来追踪网页不调用原生方法的问题
        Logc.i(TAG, this.getClass().getSimpleName() + ",onRestoreInstanceState savedInstanceState:" + (savedInstanceState != null));
    }
}
